package com.xuecheng.manage_cms.service;

import com.xuecheng.framework.domain.cms.CmsSite;
import com.xuecheng.framework.domain.cms.CmsTemplate;
import com.xuecheng.framework.domain.cms.CmsTemplate;
import com.xuecheng.framework.domain.cms.request.QueryTemplateRequest;
import com.xuecheng.framework.domain.cms.response.CmsCode;
import com.xuecheng.framework.domain.cms.response.CmsTemplateResult;
import com.xuecheng.framework.exception.ExceptionCast;
import com.xuecheng.framework.model.response.CommonCode;
import com.xuecheng.framework.model.response.QueryResponseResult;
import com.xuecheng.framework.model.response.QueryResult;
import com.xuecheng.framework.model.response.ResponseResult;
import com.xuecheng.manage_cms.dao.CmsSiteRepository;
import com.xuecheng.manage_cms.dao.CmsTemplateRepository;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @author zhaoxiang
 * @version 1.0
 * @Date 2019/10/4 18:23
 */
@Service
public class CmsTemplateService {

    @Autowired
    private CmsTemplateRepository cmsTemplateRepository;
    @Autowired
    private CmsSiteRepository cmsSiteRepository;

    /**
     * 模板列表分页查询
     * @param page
     * @param size
     * @param queryTemplateRequest
     * @return
     */
    public QueryResponseResult findList(int page, int size, QueryTemplateRequest queryTemplateRequest){
        if (queryTemplateRequest == null){
            queryTemplateRequest = new QueryTemplateRequest();
        }
        //条件匹配器
        //模板名称模糊
        ExampleMatcher exampleMatcher = ExampleMatcher.matching()
                .withMatcher("templateName" +
                        "", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("siteId", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("templateFileId", ExampleMatcher.GenericPropertyMatchers.contains());
        //条件值
        CmsTemplate cmsTemplate = new CmsTemplate();
        //站点id
        if (StringUtils.isNotEmpty(queryTemplateRequest.getSiteId())){
            cmsTemplate.setSiteId(queryTemplateRequest.getSiteId());
        }
        //模板名称
        if(StringUtils.isNotEmpty(queryTemplateRequest.getTemplateName())){
            cmsTemplate.setTemplateName(queryTemplateRequest.getTemplateName());
        }
        //模板文件id
        if (StringUtils.isNotEmpty(queryTemplateRequest.getTemplateFileId())) {
            cmsTemplate.setTemplateFileId(queryTemplateRequest.getTemplateFileId());
        }
        //创建条件实例
        Example<CmsTemplate> example = Example.of(cmsTemplate, exampleMatcher);
        //页码
        if(page <= 0){
            page=1;
        }
        page = page-1;
        if(size <= 0 ){
            size = 20;
        }
        //分页对象
        Pageable pageable = new PageRequest(page,size);
        //自定义条件分页查询
        Page<CmsTemplate> all = cmsTemplateRepository.findAll(example, pageable);
        QueryResult<CmsTemplate> cmsTemplateQueryResult = new QueryResult<>();
        cmsTemplateQueryResult.setList(all.getContent());
        cmsTemplateQueryResult.setTotal(all.getTotalElements());
        return new QueryResponseResult(CommonCode.SUCCESS,cmsTemplateQueryResult);
    }

    /**
     * 模板新增页面
     * @param cmsTemplate
     * @return
     */
    public CmsTemplateResult  add(CmsTemplate cmsTemplate){
        //检验模板名称、站点id、模板文件id
        CmsTemplate rCmsTemplate = cmsTemplateRepository.findByTemplateNameAndSiteIdAndTemplateFileId(cmsTemplate.getTemplateName(), cmsTemplate.getSiteId(), cmsTemplate.getTemplateFileId());
        if (rCmsTemplate != null){
            //抛出自定义异常，提示页面已存在
            ExceptionCast.cast(CmsCode.CMS_TEMPLATE_NOTEXIST);
        }
        cmsTemplateRepository.save(cmsTemplate);
        return new CmsTemplateResult(CommonCode.SUCCESS, cmsTemplate);
    }

    /**
     * 根据id查询页面信息
     * @param templateId
     * @return
     */
    public CmsTemplate findByTemplateId(String templateId){
        Optional<CmsTemplate> optional = cmsTemplateRepository.findById(templateId);
        if (optional.isPresent()){
            return optional.get();
        }
        return null;
    }

    /**
     * 修改页面
     * @param templateId
     * @param cmsTemplate
     * @return
     */
    public CmsTemplateResult update(String templateId, CmsTemplate cmsTemplate){
        CmsTemplate rCmsTemplate = this.findByTemplateId(templateId);
        if (rCmsTemplate != null){
            //更新站点id
            rCmsTemplate.setSiteId(cmsTemplate.getSiteId());
            //更新模板名称
            rCmsTemplate.setTemplateName(cmsTemplate.getTemplateName());
            //更新模板参数
            rCmsTemplate.setTemplateParameter(cmsTemplate.getTemplateParameter());
            //更新模板文件id
            rCmsTemplate.setTemplateFileId(cmsTemplate.getTemplateFileId());
            CmsTemplate save = cmsTemplateRepository.save(rCmsTemplate);
            if (save != null) {
                //返回成功
                CmsTemplateResult CmsTemplateResult = new CmsTemplateResult(CommonCode.SUCCESS, save);
                return CmsTemplateResult;
            }
        }
        //返回失败
        return new CmsTemplateResult(CommonCode.FAIL,null);
    }

    /**
     * 删除页面
     * @param templateId
     * @return
     */
    public ResponseResult delete(String templateId){
        CmsTemplate rCmsTemplate = this.findByTemplateId(templateId);
        if (rCmsTemplate != null){
            //删除页面
            cmsTemplateRepository.delete(rCmsTemplate);
            return new ResponseResult(CommonCode.SUCCESS);
        }
        return new ResponseResult(CommonCode.FAIL);
    }

    /**
     * 查询站点下拉菜单列表
     * @return
     */
    public QueryResponseResult findSiteList(){
        List<CmsSite> all = cmsSiteRepository.findAll();
        QueryResult<CmsSite> cmsSiteQueryResult = new QueryResult<>();
        cmsSiteQueryResult.setList(all);
        return new QueryResponseResult(CommonCode.SUCCESS,cmsSiteQueryResult);
    }
}
